<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\Order;
use app\model\OrderGoods;
use app\model\Member;
use app\model\Comment;

class CommentController extends Base
{
    public function add()
    {
        $Membermob = new Member;
        $memberinfo = $Membermob->getUserByWechat();

        $postJsonString = input("post.postJsonString");
        $postdata = json_decode($postJsonString, true);
        foreach ($postdata['goods'] as $vo) {
            Comment::create([
                'uniacid' => uniacid(),
                'uid' => $memberinfo['id'],
                'nick_name' => $memberinfo['nickname'],
                'head_img_url' => $memberinfo['userpic'],
                'status' => 0,
                'order_id' => $vo['order_id'],
                'goods_id' => $vo['goods_id'],
                'level' => $vo['level'],
                'content' => $vo['content'],
                'create_time' => time(),
                'update_time' => time()
            ]);
        }
        Order::update(['is_comment' => 1, 'id' => $postdata['orderInfo']['id']]);
        return $this->json(['msg' => '感谢您的评价', 'data' => $data]);
    }

    public function getorder()
    {
        $id = input('get.id');
        $data = Order::order_info($id);

        if (!empty($data['orderInfo']['cat_id'])) {
            $data['orderInfo']['cateMap']['image'] = Category::getImage($data['orderInfo']['cat_id']);
        } else {
            foreach ($data['goods'] as &$vo) {
                $vo['goods']['image'] = toimg($vo['goods']['image']);
            }
        }
        if ($data['orderInfo']['additional'] > 0 && $data['orderInfo']['additional_pay_time'] == 0) {
            $tmplIds = Config::getconfig('subscribemessage');
            $data['tmplIds'][] = $tmplIds['complete_tpl'];
        }

        $addressdata = OrderAddress::where(['order_id' => $id, 'ptype' => 1])->find();
    
    
        if (empty($addressdata)) {
            $addressdata = Address::find($data['orderInfo']['address_id']);
        }
        if (!empty($addressdata)) {
            $addressdata = $addressdata->toArray();
            $data['orderInfo']['address'] = $addressdata;
        }

        if ($data['orderInfo']['is_errands'] == 1) {

            $takeaddressdata = OrderAddress::where(['order_id' => $id, 'ptype' => 2])->find();

            if (empty($takeaddressdata)) {
                $takeaddressdata = Address::find($data['orderInfo']['take_address_id']);
            }

            if (!empty($takeaddressdata)) {
                $takeaddressdata = $takeaddressdata->toArray();

                $data['orderInfo']['take_address'] = $takeaddressdata;
            }
        }

        return $this->json(['data' => $data]);
    }

    public function getgoodscomment()
    {

        $goods_id = input("get.goodsId");
        $where['goods_id'] = $goods_id;
        $where['status'] = 1;
        $data = Comment::where($where)->order('id asc')->select()->toArray();
        foreach ($data as &$vo) {
            $vo['nick_name'] = substr_cut($vo['nick_name']);
        }
        return $this->json(['data' => $data]);
    }

    public function getgoods()
    {

        $goods_id = input("get.goodsId");
        $OrderGoods = new OrderGoods;

        $data = $OrderGoods->getOrderGoods($goods_id);


        foreach ($data as &$vo) {
            $vo['level'] = 5;
            $vo['content'] = '';
        }
        return $this->json(['data' => $data]);
    }
}
